<?php
function post($var, $autoSlash = true) {
	$val = ((isset($_POST[$var])) ? $_POST[$var] : ((isset($HTTP_POST_VARS[$var])) ? $HTTP_POST_VARS[$var] : '') );
	if(!get_magic_quotes_gpc() && $autoSlash && !is_array($val)) {
		if(!is_array($val)){
			$val = addslashes($val);
		}
	}
	
	return $val;
}

function post_by_key($arr_key=array(), $prefix='', $autoSlash = true) {
	$arr_result	= array();
	for($i=0, $max_i=count($arr_key); $i<$max_i; $i++){
		$var	= $arr_key[$i];
		$val = ((isset($_POST[$var])) ? $_POST[$var] : ((isset($HTTP_POST_VARS[$var])) ? $HTTP_POST_VARS[$var] : '') );
		if(!get_magic_quotes_gpc() && $autoSlash && !is_array($val)) {
			$val = addslashes($val);
		}
		$arr_result[$var]	= $val;
	}
	
	return $arr_result;
}

function post_data($var, $autoSlash = true) {
	$val = ((isset($_POST[$var])) ? $_POST[$var] : ((isset($HTTP_POST_VARS[$var])) ? $HTTP_POST_VARS[$var] : '') );
	
	if(isset($val) && is_array($val)){
		$arr_model = array_keys($val);
		for($i=0, $max_i=count($arr_model); $i<$max_i; $i++) {
			$modelName = $arr_model[$i];
			
			$arr_field = array_keys( $val[$modelName] );
			for($j=0, $max_j=count($arr_field); $j<$max_j; $j++) {
				$fieldName = $arr_field[$j];
				
				if(!get_magic_quotes_gpc() && $autoSlash) {
					$val[$modelName][$fieldName] = addslashes( $val[$modelName][$fieldName] );
				}
			}
			unset($arr_field);
		}
		unset($arr_model);
	}
	
	return $val;
}

function get($var, $autoSlash = true) {
	$val = ((isset($_GET[$var])) ? $_GET[$var] : ((isset($HTTP_GET_VARS[$var])) ? $HTTP_GET_VARS[$var] : '') );
	if(!get_magic_quotes_gpc() && $autoSlash) {
		$val = addslashes($val);
	}
	
	return $val;
}

function cookie($var, $autoSlash = false) {
	$val = ((isset($_COOKIE[$var])) ? $_COOKIE[$var] : ((isset($HTTP_COOKIE_VARS[$var])) ? $HTTP_COOKIE_VARS[$var] : '') );
	if(!get_magic_quotes_gpc() && $autoSlash) {
		$val = addslashes($val);
	}
	
	return $val;
}

function session($var, $autoSlash = false) {
	$val = ((isset($_SESSION[$var])) ? $_SESSION[$var] : ((isset($HTTP_SESSION_VARS[$var])) ? $HTTP_SESSION_VARS[$var] : '') );
	if(!get_magic_quotes_gpc() && $autoSlash) {
		$val = addslashes($val);
	}
	
	return $val;
}

function postfile($var, $autoSlash = false) {
	$val = ((isset($_FILES[$var])) ? $_FILES[$var] : ((isset($HTTP_POST_FILES[$var])) ? $HTTP_POST_FILES[$var] : '') );
	if(!get_magic_quotes_gpc() && $autoSlash) {
		$val = addslashes($val);
	}
	
	return $val;
}

function is_email($string)
{
	/* Example : admin@cake.net
	  so :	$len_mailuser	= 5 ('admin')
			$len_maildomain	= 4 ('cake')
			$len_mailtail	= 3 ('net')
	*/
	
	$len_mailuser	= strpos($string,"@");
	$len_maildomain	= strpos($string,".") - strpos($string,"@") - 1;
	$len_mailtail	= strlen($string) - strpos($string,".") - 1;
	
	$res = false;
	
	if(@ereg("[^a-zA-Z0-9@._]", $string)) {
		$res = false; 
	}else {
		if(strstr($string,"@")!="" && strstr($string,".")!="" && strpos($string,"@")<strpos($string,".") && 
			$len_mailuser>2 && $len_maildomain>3 && $len_mailtail>1)
			$res = true;
		else
			$res = false;
	}
	
	return $res;
}
?>